/*
 * @Author: Suo-Yuheng syh15399227325@163.com
 * @Date: 2025-09-08 23:11:56
 * @LastEditors: Suo-Yuheng syh15399227325@163.com
 * @LastEditTime: 2025-09-08 23:29:40
 * @FilePath: /0907/tcp/Init.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "log.hpp"

using namespace std;

extern log lg;


const string dictname = "./dict.txt";
const string sep = ":";

static bool Split(string &line, string &part1, string &part2)
{
    auto pos = line.find(sep);
    if(pos == string::npos) return false;
    part1 = line.substr(0,pos);
    part2 = line.substr(pos+1);
    return true;
}

class Init
{
public:
    Init()
    {
        ifstream in(dictname);
        if(!in.is_open())
        {
            lg(Fatal, "ifstream open %s error", dictname.c_str());
            exit(1);
        }

        string line;
        while(getline(in, line))    // getline 的返回值,做了 bool 类型的重载
        {
            string part1, part2;
            Split(line, part1, part2);
            dict.insert({part1, part2});
        }

        in.close();
    }

    string translate(const string &key)
    {
        auto it = dict.find(key);
        if(it == dict.end()) return "None";
        return (*it).second;
    }

private:
    unordered_map<string, string> dict;
};